package com.roocon.design_pattern.chapter05_guard_suspension;

import java.util.LinkedList;

public class RequestQueue {
  private final LinkedList<Request> queue = new LinkedList<>();

  public void putRequest(Request request) {
    synchronized (queue) {
      queue.addLast(request);
      queue.notifyAll();
    }
  }

  public Request getRequest() {
    synchronized (queue) {
      while (queue.size() == 0) {
        try {
          queue.wait();
        } catch (InterruptedException e) {
          return null;
        }
      }
      return queue.removeFirst();
    }
  }
}
